﻿using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using System.Text;
using System.Collections.Generic;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Publishing;

namespace PageLayoutChange
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter top level site name:");
           string siteName = Console.ReadLine();
           if (string.IsNullOrEmpty(siteName))
           {
               Console.WriteLine("Site does not exist or was not provided");
           }

            SPSite site = new SPSite(siteName);

            if (site != null)
            {
                
                
                
                Console.Write("Please enter website name containing Page: ");
                string webName = Console.ReadLine();
                if (string.IsNullOrEmpty(webName))
                {
                    Console.WriteLine("Web does not exist or was not provided");
                }
                
                Console.Write("Please enter name of library: ");
                string libraryName = Console.ReadLine();
                if (string.IsNullOrEmpty(libraryName))
                {
                    Console.WriteLine("Library does not exist or was not provided");
                }
                Console.Write("Please enter name of Page Layout you wish to change to: ");
                string pageLayoutName = Console.ReadLine();
                if (string.IsNullOrEmpty(pageLayoutName))
                {
                    Console.WriteLine("Page Layout does not exist or was not provided");
                }
                using (SPWeb archives = site.OpenWeb(webName))
                {

                    PublishingWeb publishingweb = PublishingWeb.GetPublishingWeb(archives);
                    PageLayout pageLayout = null;
                    foreach (PageLayout p in publishingweb.GetAvailablePageLayouts())
                    {
                        if (p.Name.Equals(pageLayoutName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            pageLayout = p;
                            break;
                        }
                    }
                    SPList pages = archives.Lists[libraryName];
                    foreach (SPListItem item in pages.Items)
                    {
                        if (item.Name != "default.aspx")
                        {
                            PublishingPage page = publishingweb.GetPublishingPage(archives.ServerRelativeUrl + "/" + libraryName + "/" + item.Name);
                            page.CheckOut();
                            page.Layout = pageLayout;
                            page.Update();
                            page.CheckIn("Page Layout Changed");
                            Console.WriteLine("Page Layout Changed for:" + item.Name);
                        }
                    }
                    Console.WriteLine("All Page Layouts successfully changed");
                }
            }

        }
    }
}
